package com.zhn;

//给定一个非空二叉树, 返回一个由每层节点平均值组成的数组。

import java.util.*;

public class AverageOfLevels {

    List<Double> result = new ArrayList<>();
    public List<Double> averageOfLevels(TreeNode root) {
        travel(root);
        return result;
    }

    public void travel(TreeNode root){
        if(root == null){
            return;
        }
        Queue<TreeNode> que = new LinkedList<>();
        que.add(root);
        while (!que.isEmpty()){
            Double sum = 0.0;
            int len = que.size();
            int t = len;
            while(len-->0){
                TreeNode poll = que.poll();
                sum+=poll.val;
                if(poll.left != null) que.add(poll.left);
                if(poll.right != null)que.add(poll.right);
            }
            result.add(sum / t);
        }
    }
}
